<template>
  <div class="app-container">
    <eHeader
      ref="header"
      :query="query"
      :eqpt-id.sync="eqptId"
      :sup-this="supThis"
    />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <!--<el-table-column prop="id" label="序号" />-->
      <el-table-column :show-overflow-tooltip="true" prop="modelNo" label="配件编号" />
      <el-table-column :show-overflow-tooltip="true" prop="modelName" label="名称" />
      <el-table-column :show-overflow-tooltip="true" prop="modelModel" label="型号" />
      <el-table-column :show-overflow-tooltip="true" prop="modelSerialno" label="序列号" />
      <el-table-column :show-overflow-tooltip="true" prop="notes" label="备注" />
      <el-table-column v-if="checkPermission(['ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_EDIT','TEQUIPMENT_DELETE'])" fixed="right" label="操作" width="280px" align="center">
        <template slot-scope="scope">
          <edit v-permission="['ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_EDIT']" :data="scope.row" :sup-this.sync="supThis" :is-view="true" :is-add="false" />
          <edit v-permission="['ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_EDIT']" :data="scope.row" :sup-this.sync="supThis" :is-view="false" :is-add="false" />
          <modelqrcode v-permission="['ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_EDIT']" :data="scope.row" :sup-this="supThis" />
          <el-popover
            :ref="scope.row.id"
            v-permission="['ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_DELETE']"
            placement="top"
            width="180"
          >
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" />
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { delModel } from '@/api/tEquipment'
import { parseTimeShort } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import { SPARE_STATES } from '@/constants/consts'
import modelqrcode from './module/modelqrcode.vue'
export default {
  components: { eHeader, edit, modelqrcode },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      supThis: this,
      eqptId: 0
    }
  },
  created() {
    this.loading = false
  },
  methods: {
    parseTimeShort,
    checkPermission,
    beforeInit() {
      // 没有设备标识不查询
      if (!this.eqptId) {
        return false
      }
      this.url = 'api/tEquipment/model'
      const sort = 'id,asc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query

      if (this.eqptId) {
        this.params['eqptId'] = this.eqptId
      }
      if (query.modelName) {
        this.params['modelName'] = query.modelName
      }
      if (query.modelNo) {
        this.params['modelNo'] = query.modelNo
      }
      if (query.modelModel) {
        this.params['modelModel'] = query.modelModel
      }
      if (query.modelSerialno) {
        this.params['modelSerialno'] = query.modelSerialno
      }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      delModel(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    },
    formatState(row) {
      for (var index in SPARE_STATES) {
        const obj = SPARE_STATES[index]
        if (obj.id === row['state']) {
          return obj.name
        }
      }
      return row['state']
    }
  }
}
</script>

<style scoped>

</style>
